package com.smzd.framework.system.business;

import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smzd.core.dao.IDao;
import com.smzd.framework.core.business.AbstractFGService;
import com.smzd.framework.system.dao.MenuDao;
import com.smzd.framework.system.domain.Function;
import com.smzd.framework.system.domain.Menu;
import com.smzd.framework.utils.JsonConverter;
import com.smzd.framework.utils.JsonHelper;
import com.smzd.framework.utils.db.FGRowConverter;
import com.smzd.framework.utils.db.RPage;

@Service
public class MenuService extends AbstractFGService<Menu> {
	@Autowired
	private MenuDao dao;

	@Override
	protected IDao<Menu> getDao() {
		return dao;
	}

	@Override
	public FGRowConverter<Menu> getConverter() {
		return converter;
	}
	
	private static final FGRowConverter<Menu> converter = new FGRowConverter<Menu>() {
		@Override
		public Object[] convert(Menu elem) {
			Menu scope = elem.getScope();
			Function func = elem.getReference();
			Object[] fields = new Object[]{
					elem.getId(),
					elem.getName(),
					elem.getDescription(),
					scope==null?null:scope.getId(),
					scope==null?null:scope.getName(),
					func==null?null:func.getId(),
					func==null?null:func.getName(),
					func==null?null:func.getEntry(),
					elem.getSequence()
			};
			return fields;
		}
	};

	private static final FGRowConverter<Menu> jdMenuConverter = new FGRowConverter<Menu>() {
		@Override
		public Object[] convert(Menu elem) {
			Menu scope = elem.getScope();
			Function func = elem.getReference();
			Object[] fields = new Object[]{
					elem.getId(),
					elem.getName(),
					scope==null?null:scope.getId(),
					func==null?null:func.getEntry()
			};
			return fields;
		}
	};
	
	public String getMenuJsonByUserId(int userId){
		List<Menu> menus = dao.getMenusByUserId(userId);
		RPage<Menu> page = new RPage<Menu>();
		page.setElems(menus);
		page.setTotalCount(menus.size());
		return page.toFlexiGridJsonString(jdMenuConverter);
	}
	public String getMenuJsonByMeetingId(int meetingId) {
		List<Menu> menus = dao.getMenusByMeetingId(meetingId);
		RPage<Menu> page = new RPage<Menu>();
		page.setElems(menus);
		page.setTotalCount(menus.size());
		return page.toFlexiGridJsonString(jdMenuConverter);
	}

	public String getMenuInfoById(int id) {
		Menu menu = dao.getElemById(id);
		final JsonConverter<Menu> menuInfoConverter = new JsonConverter<Menu>() {
			@Override
			protected void accumulate(JSONObject json, Menu elem) {
				Menu scope = elem.getScope();
				Function func = elem.getReference();
				json.accumulate("id", elem.getId());
				json.accumulate("name", elem.getName());
				json.accumulate("desc", elem.getDescription());
				json.accumulate("pid", scope==null?null:scope.getId());
				json.accumulate("seq", elem.getSequence());
				json.accumulate("mid", func==null?null:func.getScope().getId());
				json.accumulate("mcid", func==null?null:func.getId());
			}
		};
		return menuInfoConverter.toJsonString(menu);
	}

	public String updateMenuPosition(Menu elem) {
		dao.updateMenuPosition(elem);
		return JsonHelper.getSuccessJsonString();
	}

}
